/*

 * Copyright (C) 2021 Huawei Device Co., Ltd.

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *     http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */
package io.karim.materialtabs.sample;


import io.karim.MaterialTabs;
import ohos.agp.components.*;
import ohos.app.Context;

import java.util.ArrayList;

/**
 * ViewPagerAdapter
 *
 * @author hal
 * @since 2021/06/21
 */
public class ViewPagerAdapter extends PageSliderProvider implements MaterialTabs.CustomTabProvider {
    private final Context context;
    private final String[] TITLES = {"Tabs", "Ripple"};

    private final int[] UNSELECTED_ICONS = {ResourceTable.Media_ic_tabs_unselected, ResourceTable.Media_ic_ripple_unselected};
    private final int[] SELECTED_ICONS = {ResourceTable.Media_ic_tabs_selected, ResourceTable.Media_ic_ripple_selected};
    private final TabsSettingsFragment tabsSettingsFragment;
    private final RippleSettingsFragment rippleSettingsFragment;

    public ViewPagerAdapter(Context context, ArrayList<ResettableFragment> mFragments) {
        this.context = context;
        tabsSettingsFragment = new TabsSettingsFragment(context);
        rippleSettingsFragment = new RippleSettingsFragment(context);
        mFragments.add(tabsSettingsFragment);
        mFragments.add(rippleSettingsFragment);
    }

    @Override
    public String getPageTitle(int position) {
        return TITLES[position];
    }

    @Override
    public int getCount() {
        return TITLES.length;
    }

    @Override
    public Object createPageInContainer(ComponentContainer componentContainer, int position) {
        if (position == 0) {
            componentContainer.addComponent(tabsSettingsFragment);
            return tabsSettingsFragment;
        } else {
            componentContainer.addComponent(rippleSettingsFragment);
            return rippleSettingsFragment;
        }
    }


    @Override
    public void destroyPageFromContainer(ComponentContainer componentContainer, int position, Object object) {
        componentContainer.removeComponent((Component) object);
    }

    @Override
    public boolean isPageMatchToObject(Component component, Object object) {
        return true;
    }

    @Override
    public Component getCustomTabView(ComponentContainer parent, int position) {
        Image imageView = new Image(context);
        imageView.setPixelMap(UNSELECTED_ICONS[position]);
        return imageView;
    }

    @Override
    public void onCustomTabViewSelected(Component view, int position, boolean alreadySelected) {
        if (view instanceof Image) {
            ((Image) view).setPixelMap(SELECTED_ICONS[position]);
        }
    }

    @Override
    public void onCustomTabViewUnselected(Component view, int position, boolean alreadyUnselected) {
        if (view instanceof Image) {
            ((Image) view).setPixelMap(UNSELECTED_ICONS[position]);
        }
    }

}